package cn.com.dcsgo.sdk.payment.wechat.ltzf.factory.defaults;

import cn.com.dcsgo.sdk.payment.wechat.ltzf.factory.Configuration;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.factory.PayFactory;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.h5.H5PayService;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.h5.IH5PayApi;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.jsapi.IJsapiApi;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.jsapi.JsapiPayService;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.jumph5.IJumpH5PayApi;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.jumph5.JumpH5PayService;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.nativepay.INativePayApi;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.nativepay.NativePayService;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.util.concurrent.TimeUnit;

/**
 * 支付工厂，接口实现
 *
 * @author Dcsgo
 * @since 2024/8/13 上午10:06
 */

public class DefaultPayFactory implements PayFactory {
    /**
     * 配置类(蓝兔支付、HTTP相关配置)
     */
    private final Configuration configuration;

    /**
     * HTTP客户端，用于发送 HTTP 请求和接收 HTTP 响应
     */
    private final OkHttpClient httpClient;

    /**
     * 通过配置类构造一个支付工厂
     *
     * @param configuration 配置类(蓝兔支付、HTTP相关配置)
     */
    public DefaultPayFactory(Configuration configuration) {
        this.configuration = configuration;
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(configuration.getLevel());
        this.httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .connectTimeout(configuration.getConnectTimeout(), TimeUnit.SECONDS)
                .writeTimeout(configuration.getWriteTimeout(), TimeUnit.SECONDS)
                .readTimeout(configuration.getReadTimeout(), TimeUnit.SECONDS)
                .build();
    }

    @Override
    public NativePayService nativePayService() {
        INativePayApi nativePayApi = new Retrofit.Builder()
                .baseUrl(configuration.getApiHost())
                .client(httpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build()
                .create(INativePayApi.class);
        return new NativePayService(nativePayApi, configuration);
    }

    @Override
    public H5PayService h5PayService() {
        IH5PayApi h5PayApi = new Retrofit.Builder()
                .baseUrl(configuration.getApiHost())
                .client(httpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build()
                .create(IH5PayApi.class);
        return new H5PayService(h5PayApi, configuration);
    }

    @Override
    public JumpH5PayService jumpH5PayService() {
        IJumpH5PayApi jumpH5PayApi = new Retrofit.Builder()
                .baseUrl(configuration.getApiHost())
                .client(httpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build()
                .create(IJumpH5PayApi.class);
        return new JumpH5PayService(jumpH5PayApi, configuration);
    }

    @Override
    public JsapiPayService jsapiPayService() {
        IJsapiApi jsapiApi = new Retrofit.Builder()
                .baseUrl(configuration.getApiHost())
                .client(httpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build()
                .create(IJsapiApi.class);
        return new JsapiPayService(jsapiApi, configuration);
    }
}
